package com.veryedu.hibernate.biz;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.veryedu.hibernate.biz.bean.DateBean;
import com.veryedu.hibernate.biz.bean.FilmTimeBean;
import com.veryedu.hibernate.biz.bean.SeatBean;
import com.veryedu.hibernate.biz.bean.SeatsBean;
import com.veryedu.hibernate.biz.bean.TicketBean;
import com.veryedu.hibernate.biz.bean.TimeBean;
import com.veryedu.hibernate.commons.Constant;
import com.veryedu.hibernate.exception.BizzException;
import com.veryedu.hibernate.hbm.dao.FilmDao;
import com.veryedu.hibernate.hbm.dao.OrderDao;
import com.veryedu.hibernate.hbm.dao.SeatDao;
import com.veryedu.hibernate.hbm.dao.ShowTimeDao;
import com.veryedu.hibernate.hbm.dao.TicketsDao;
import com.veryedu.hibernate.hbm.dao.UserDao;
import com.veryedu.hibernate.hbm.entity.Film;
import com.veryedu.hibernate.hbm.entity.Order;
import com.veryedu.hibernate.hbm.entity.Room;
import com.veryedu.hibernate.hbm.entity.Seat;
import com.veryedu.hibernate.hbm.entity.ShowTime;
import com.veryedu.hibernate.hbm.entity.Tickets;
import com.veryedu.hibernate.hbm.entity.User;

public class DPBiz {
	private FilmDao filmDao;
	private OrderDao orderDao;
	private SeatDao seatDao;
	private ShowTimeDao showTimeDao;
	private TicketsDao ticketsDao;
	private UserDao userDao;
		
	public FilmDao getFilmDao() {
		return filmDao;
	}

	public void setFilmDao(FilmDao filmDao) {
		this.filmDao = filmDao;
	}

	public OrderDao getOrderDao() {
		return orderDao;
	}

	public void setOrderDao(OrderDao orderDao) {
		this.orderDao = orderDao;
	}

	public SeatDao getSeatDao() {
		return seatDao;
	}

	public void setSeatDao(SeatDao seatDao) {
		this.seatDao = seatDao;
	}

	public ShowTimeDao getShowTimeDao() {
		return showTimeDao;
	}

	public void setShowTimeDao(ShowTimeDao showTimeDao) {
		this.showTimeDao = showTimeDao;
	}

	public TicketsDao getTicketsDao() {
		return ticketsDao;
	}

	public void setTicketsDao(TicketsDao ticketsDao) {
		this.ticketsDao = ticketsDao;
	}

	public UserDao getUserDao() {
		return userDao;
	}

	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}

	public FilmTimeBean filmTime(Integer filmId) {

		FilmTimeBean filmTime = new FilmTimeBean();
		Film film = (Film) filmDao.getByPk(Film.class, filmId);

		filmTime.setFilmId(film.getFilmid());
		filmTime.setFilmName(film.getFilmname());
		filmTime.setFilmImg(film.getFilmimg());

		HashMap<String, DateBean> dateList = new HashMap<String, DateBean>();
		List<DateBean> dates = new ArrayList<DateBean>();

		for (Object obj : film.getShowTimes()) {
			ShowTime st = (ShowTime) obj;
			String date = Constant.YYYY_MM_DD.format(st.getPlaytime());
			if (dateList.containsKey(date)) {

				DateBean dateBean = (DateBean) dateList.get(date);
				List<TimeBean> times = dateBean.getTimes();

				TimeBean timeBean = new TimeBean();
				timeBean.setShowTimeId(st.getChangid());
				timeBean.setTime(Constant.HH_MM.format(st.getPlaytime()));
				timeBean.setRoomname(st.getRoom().getRoomname());
				timeBean.setPrice("￥" + st.getPrice());
				times.add(timeBean);

			} else {

				DateBean dateBean = new DateBean();
				List<TimeBean> times = new ArrayList<TimeBean>();

				TimeBean timeBean = new TimeBean();
				timeBean.setShowTimeId(st.getChangid());
				timeBean.setTime(Constant.HH_MM.format(st.getPlaytime()));
				timeBean.setRoomname(st.getRoom().getRoomname());
				timeBean.setPrice("￥" + st.getPrice());
				times.add(timeBean);

				dateBean.setDate(date);
				dateBean.setTimes(times);

				dateList.put(date, dateBean);
			}
		}
		Iterator it = dateList.entrySet().iterator();
		while (it.hasNext()) {
			Map.Entry entry = (Map.Entry) it.next();
			DateBean date = (DateBean) entry.getValue();
			Collections.sort(date.getTimes());
			dates.add((DateBean) entry.getValue());
		}

		Collections.sort(dates);

		filmTime.setDates(dates);

		return filmTime;
	}

	public SeatsBean seatList(Integer showTimeId) {
		ShowTime st = (ShowTime) showTimeDao.getByPk(ShowTime.class, showTimeId);
		Room room = st.getRoom();
		Set<Seat> seatSet = room.getSeats();
		SeatsBean seats = new SeatsBean();
		
		HashMap<String,Float> prices = calcuPrice(st.getPrice());
		seats.setFilmId(st.getFilm().getFilmid());
		seats.setShowTimeId(st.getChangid());
		seats.setGenePrice(prices.get("genePrice"));
		seats.setTeamPrice(prices.get("teamPrice"));
		seats.setStuPrice(prices.get("stuPrice"));
		
		ArrayList<SeatBean> seatList = new ArrayList<SeatBean>();
		
		for (Object obj : seatSet) {
			Seat seat = (Seat) obj;
			SeatBean seatBean = new SeatBean();
			
			seatBean.setSeatId(seat.getSeatid());
			seatBean.setSeatName(seat.getSeatname());
			seatBean.setRowno(seat.getRowno());
			seatBean.setColno(seat.getColno());
			seatBean.setIsSold(false);
			Set<Tickets> tks = seat.getTicketses();
			for(Tickets tk : tks){
				if(tk.getShowTime().getChangid() == st.getChangid()){
					seatBean.setIsSold(true);
				}else{
					seatBean.setIsSold(false);
				}
			}
			seatList.add(seatBean);
		}
		seatList.trimToSize();
		Collections.sort(seatList);
		seats.setSeatList(seatList);
		
		return seats;
	}

	private HashMap<String, Float> calcuPrice(Float price) {
		HashMap<String, Float> prices = new HashMap<String,Float>();
		prices.put("genePrice", price);
		prices.put("teamPrice", (float) (price*0.5));
		prices.put("stuPrice", (float) (price*0.5));
		return prices;
	}

	public List<TicketBean> viewTicket(Integer filmId, Integer showTimeId, Float price,
			List<Integer> buySeats) {

		List<TicketBean> tickets = new ArrayList<TicketBean>();
		try{
			ShowTime st = (ShowTime)showTimeDao.getByPk(ShowTime.class, showTimeId);
			for(Integer seatId : buySeats){
				Seat seat = (Seat)seatDao.getByPk(Seat.class, seatId);
				List<Tickets> ticketsList = ticketsDao.findByShowTimeAndSeat(st,seat);
				if(ticketsList.isEmpty()){
					TicketBean tk = new TicketBean();
					tk.setSeatId(seat.getSeatid());
					tk.setShowTimeId(st.getChangid());
					tk.setRoomname(st.getRoom().getRoomname());
					tk.setFilmname(st.getFilm().getFilmname());
					tk.setSeatname(seat.getSeatname());
					tk.setDate(Constant.YYYY_MM_DD.format(st.getPlaytime()));
					tk.setTime(Constant.HH_MM.format(st.getPlaytime()));
					tk.setPrice(price + "元");
					tickets.add(tk);
				}else{
					//
				}
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		return tickets;
	}

	public Order createOrder(String userName, String password,
			Integer showTimeId, Float price, Float totalPrice, List<Integer> buySeats) throws BizzException {
		
		List<User> users = (List<User>)userDao.findByProperty(User.class, "username", userName);
		User user = new User();
		
		Order order = null;
		
		if(users.isEmpty()){
			//用户不存在
			BizzException bizzExcp = new BizzException();
			bizzExcp.setErrorMsg("用户不存在");
			throw bizzExcp;
		}else{
			boolean isPass = false;
			for(User u : users){
				if(u.getPassword().equals(password)){
					isPass = true;
					user = u;
					break;
				}
			}
			
			if(isPass){
				if(user.getMoney() < totalPrice){
					//余额不够
					BizzException bizzExcp = new BizzException();
					bizzExcp.setErrorMsg("余额不够");
					throw bizzExcp;
				}else{
										
					ShowTime st = (ShowTime)showTimeDao.getByPk(ShowTime.class, showTimeId);
					Film film = st.getFilm();
					
					order = new Order();
					
					order.setOrdertime(new Timestamp(System.currentTimeMillis()));
					order.setTotalmoney(totalPrice);
					order.setUser(user);
					Set<Tickets> tks = new HashSet<Tickets>();
					for(Integer seatId : buySeats){
						Seat seat = (Seat)seatDao.getByPk(Seat.class, seatId);
						Tickets tk = new Tickets();
						tk.setPrice(price);
						tk.setSeat(seat);
						tk.setShowTime(st);
						tk.setState(0);
						tks.add(tk);
					}
					order.setTicketses(tks);
					order = (Order)orderDao.add(order);
					
					user.setMoney(user.getMoney()-totalPrice);
					userDao.update(user);
				}
			}else{
				//密码不正确
				BizzException bizzExcp = new BizzException();
				bizzExcp.setErrorMsg("密码不正确");
				throw bizzExcp;
			}
		}
		return order;
	}
}
